<?php
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');
// 响应类型  
header('Access-Control-Allow-Methods:POST');
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
// require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';
class Bed extends CI_Controller{

    public function __construct(){

        parent::__construct();
        $this->load->model('Bed/Bed_manage');
        $this->load->helper('url_helper');
        $this->load->library('session');
        // $this->load->helper('date');
        $this->load->helper(array('form', 'url', 'date'));
    }

    //床位管理查询方法bed_select
    public function bed_select(){

        $jobNumber = $_POST['jobNumber'];       //工号
        $name = $_POST['name'];                 //姓名
        $department = $_POST['department'];     //部门
        $room_number = $_POST['room_number'];   //宿舍房间号
        $limit = $_POST['page'];                //页数

        // $jobNumber = "";
        // $name = "";
        // $department = "IT";
        // $room_number = "";
        // $limit = "";

        $count = $this->Bed_manage->bed_select_all(
            $jobNumber,
            $name,
            $department,
            $room_number,
            $limit
        );

        $result_json = $this->Bed_manage->bed_select(
            $jobNumber,
            $name,
            $department,
            $room_number,
            $limit,
            $count
        );

        echo $result_json;

        return 0;
    }

    //房间空床位查询
    public function bed_select_people(){

        $area_number = $_POST['area_number'];   //区域编号
        $floor_number = $_POST['floor_number']; //宿舍楼编号
        $room_number = $_POST['room_number'];   //宿舍房间号

        // $area_number = "1585";
        // $floor_number = "1";
        // $room_number = "437";

        $result_json = $this->Bed_manage->bed_select_people(
          $area_number,
          $floor_number,
          $room_number
        );

        echo $result_json;

        return 0;
    }

    //床位管理更换床位方法
    public function bed_change(){

        $area_number = $_POST['area_number'];   //区域编号
        $floor_number = $_POST['floor_number']; //宿舍楼编号
        $room_number = $_POST['room_number'];   //宿舍房间号
        $bedNumber = $_POST['bedNumber'];       //床位号
        $jobNumber = $_POST['jobNumber'];       //工号

        // $area_number = "1585";
        // $floor_number = "1";
        // $room_number = "601-2";
        // $bedNumber = "3";
        // $jobNumber = "IT00053";

        $result_json = $this->Bed_manage->bed_change(
            $area_number,
            $floor_number,
            $room_number,
            $bedNumber,
            $jobNumber
        );

        echo $result_json;

        return 0;
    }

    //床位管理修改方法
    public function bed_update(){

        $name = $_POST['name'];             //姓名
        $jobNumber = $_POST['jobNumber'];   //工号
        $phone = $_POST['phone'];           //联系方式
        $remarks = $_POST['remarks'];       //备注

        // $name = "陈羿华";
        // $jobNumber = "IT00053"; //人员编号
        // $phone = "15968914304"; //联系方式
        // $remarks = "备注"; //备注信息

        $result_json = $this->Bed_manage->bed_update(
            $name,
            $jobNumber,
            $phone,
            $remarks
        );

        echo $result_json;

        return 0;
    }

    //床位管理修改方法-领用物品/所有物品查询
    public function bed_article_select(){

        $name = $_POST['name'];                 //人员姓名
        $room_number = $_POST['room_number'];   //宿舍房间号
        $bedNumber = $_POST['bedNumber'];       //床位号

        // $name = "陈羿华";
        // $room_number = "602-1";   
        // $bedNumber = "4";      

        $result_json = $this->Bed_manage->bed_article_select(
            $name,
            $room_number,
            $bedNumber
        );

        echo $result_json;

        return 0;
    }


    public function excel_bed(){ 

        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  
        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','区域名称');
        $objActSheet->setCellValue('B1','宿舍楼名称');
        $objActSheet->setCellValue('C1','楼栋号');
        $objActSheet->setCellValue('D1','宿舍楼层号');
        $objActSheet->setCellValue('E1','宿舍房间号');
        $objActSheet->setCellValue('F1','房间类型');
        $objActSheet->setCellValue('G1','姓名');
        $objActSheet->setCellValue('H1','性别');
        $objActSheet->setCellValue('I1','工号');
        $objActSheet->setCellValue('J1','部门');
        $objActSheet->setCellValue('K1','入住日期');
        $objActSheet->setCellValue('L1','床位号');
        $objActSheet->setCellValue('M1','床位状态');
        $objActSheet->setCellValue('N1','床位是否住人');
        $objActSheet->setCellValue('O1','联系方式');

        $num = 2;

        $data = $this->Bed_manage->excel_bed();
        $data = json_decode($data,true);
        // print_r($data['msg']);
        // return 0;

        //若为性别，床位状态，是否住人这三个字段则进行替换
        for ($i=0; $i < count($data['msg']); $i++){ 
            //性别
            if($data['msg'][$i]['sex'] == 0) {
                $data['msg'][$i]['sex'] = '女';
            } else {
                $data['msg'][$i]['sex'] = '男';
            }
            
            //床位状态
            if($data['msg'][$i]['bedStatus'] == 0){
                $data['msg'][$i]['bedStatus'] = '未开启';
            } else {
                $data['msg'][$i]['bedStatus'] = '开启';
            }

            //床位是否住人
            if($data['msg'][$i]['check_inWhether'] == 0){
                $data['msg'][$i]['check_inWhether'] = '未住人';
            } else {
                $data['msg'][$i]['check_inWhether'] = '已住人';
            }
        }

        for ($i=0; $i < count($data['msg']); $i++) { 
            $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['area_name'] , PHPExcel_Cell_DataType::TYPE_STRING);           //区域名称
            $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['floor_name'] , PHPExcel_Cell_DataType::TYPE_STRING);          //宿舍楼名称
            $objActSheet->setCellValueExplicit('C' . $num,$data['msg'][$i]['caveNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);          //楼栋号
            $objActSheet->setCellValueExplicit('D' . $num,$data['msg'][$i]['dorm_floor'] , PHPExcel_Cell_DataType::TYPE_STRING);               //宿舍楼层号
            $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['room_number'] , PHPExcel_Cell_DataType::TYPE_STRING);         //宿舍房间号 
            $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['room_type'] , PHPExcel_Cell_DataType::TYPE_STRING);           //宿舍类型
            $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['name'], PHPExcel_Cell_DataType::TYPE_STRING);                 //姓名
            $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['sex'], PHPExcel_Cell_DataType::TYPE_STRING);                  //性别
            $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['jobNumber'], PHPExcel_Cell_DataType::TYPE_STRING);            //工号
            $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['department'], PHPExcel_Cell_DataType::TYPE_STRING);           //部门
            $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['liveDate'], PHPExcel_Cell_DataType::TYPE_STRING);             //入住日期
            $objActSheet->setCellValueExplicit('L' . $num,$data['msg'][$i]['bedNumber'], PHPExcel_Cell_DataType::TYPE_STRING);            //床位号
            $objActSheet->setCellValueExplicit('M' . $num,$data['msg'][$i]['bedStatus'], PHPExcel_Cell_DataType::TYPE_STRING);            //床位状态
            $objActSheet->setCellValueExplicit('N' . $num,$data['msg'][$i]['check_inWhether'], PHPExcel_Cell_DataType::TYPE_STRING);      //床位是否住人
            $objActSheet->setCellValueExplicit('O' . $num,$data['msg'][$i]['phone'], PHPExcel_Cell_DataType::TYPE_STRING);                //联系方式
            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }

        //设置列的属性  
        $objActSheet->getColumnDimension("A")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("G")->setWidth('20px'); // 设置宽度
        $objActSheet->getColumnDimension("H")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("I")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("J")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("K")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("L")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("M")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("N")->setWidth('20px'); // 设置宽度 
        $outputFileName = iconv('UTF-8', 'gb2312', '宿舍床位信息.xls');  
        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary');  
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
        
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $objExcel = null;
    }
}
